/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Analysis;

import java.io.*;
import jvntagger.MaxentTagger;
import jvntagger.POSTagger;
import vn.hus.nlp.tokenizer.VietTokenizer;

/**
 *
 * @author tuyentran
 */
public class VietTokenAndTag {

    public static void run(String inputFile, String outputFile) throws IOException {
        // <editor-fold desc="tokenizer">
        VietTokenizer vnTokenizer = new VietTokenizer();
        vnTokenizer.tokenize(inputFile, inputFile + ".tok");
        // </editor-fold>

        // <editor-fold desc="Tagger">
        String modelDir = "model/maxent";
        POSTagger tagger = new MaxentTagger(modelDir);
        String result = tagger.tagging(new File(inputFile + ".tok"));

        BufferedWriter bfw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "utf-8"));
        String[] taggedWords = result.split(" ");
        for (int i = 0; i < taggedWords.length; i++) {
            String[] str = taggedWords[i].split("/");
            bfw1.write(str[1]+"\t");
            bfw1.write(str[0].replace("_", " "));
            bfw1.newLine();
        }
        bfw1.close();
        // </editor-fold>
    }
    public static VietTokenizer vnTokenizer;
    public static void run(String content) throws IOException {
        // <editor-fold desc="tokenizer">
        
        if (vnTokenizer == null)
            vnTokenizer = new VietTokenizer();
        
        String[] tokenized = vnTokenizer.tokenize(content);
        String tok = joinHelper(tokenized);
        // </editor-fold>

        // <editor-fold desc="Tagger">
        String modelDir = "model/maxent";
        POSTagger tagger = new MaxentTagger(modelDir);
        String result = tagger.tagging(tok);

        String outputFile = "e://tuyentran-out.txt";
        BufferedWriter bfw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "utf-8"));
        String[] taggedWords = result.split(" ");
        for (int i = 0; i < taggedWords.length; i++) {
            String[] str = taggedWords[i].split("/");
            bfw1.write(str[1]+"\t");
            bfw1.write(str[0].replace("_", " "));
            bfw1.newLine();
        }
        bfw1.close();
        // </editor-fold>
    }
    
    private static String joinHelper(String[] str)
    {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < str.length; i++) {
           result.append( str[i] );
           //result.append( optional separator );
        }
        return result.toString();
    }
    
    public static void main(String[] args) throws IOException {
        VietTokenAndTag.run("input.txt", "output.txt");
    }
}
